from django import forms
from django.forms import widgets
from django.core.exceptions import ValidationError


class Change_Password(forms.Form):
    old_password = forms.CharField(label='旧密码', max_length=10, min_length=3, required=True,
                                   widget=widgets.PasswordInput(attrs={'class': 'layui-input', 'placeholder': "旧密码",'autocomplete':'off','lay-verify':'old'}),
                                   error_messages={'max_length': '最长是10', 'min_length': '最短是3'})
    password = forms.CharField(label='新密码', max_length=10, min_length=3, required=True,
                               widget=widgets.PasswordInput(attrs={'class': 'layui-input', 'placeholder': "新密码",'autocomplete':'off','lay-verify':'new1'}),
                               error_messages={'max_length': '最长是10', 'min_length': '最短是3'})

    re_password = forms.CharField(label='确认密码', max_length=10, min_length=3, required=True,
                                  widget=widgets.PasswordInput(attrs={'class': 'layui-input', 'placeholder': "确认密码",'autocomplete':'off','lay-verify':'new2'}),
                                  error_messages={'max_length': '最长是10'})

    def clean(self):
        pwd = self.cleaned_data.get('password')
        re_pwd = self.cleaned_data.get('re_password')
        if pwd and re_pwd:
            if pwd != re_pwd:
                raise ValidationError('两次密码不一致')
        return self.cleaned_data
